package kiki.app;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 9/18/12
 * Time: 12:11 PM
 *
 */
public class PersonFactory {

    private static ServiceRegistry serviceRegistry;
    private static final SessionFactory sessionFactory;

    static {
        try{

            Configuration configuration = new Configuration();
            configuration.addAnnotatedClass(Person.class);
            configuration.configure();
            new SchemaExport(configuration).create(true, true);
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        } catch (Throwable e){

            throw new ExceptionInInitializerError(e);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
